﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BarrageGrab
{
    public abstract class DbHelperSQLite
    {
        public static string connectionString;

        public DbHelperSQLite()
        {

        }

        public static int GetMaxID(string FieldName, string TableName)
        {
            object single = DbHelperSQLite.GetSingle("select max(" + FieldName + ")+1 from " + TableName);
            return single == null ? 1 : int.Parse(single.ToString());
        }

        public static bool Exists(string strSql)
        {
            object single = DbHelperSQLite.GetSingle(strSql);
            return (object.Equals(single, (object)null) || object.Equals(single, (object)DBNull.Value) ? 0 : int.Parse(single.ToString())) != 0;
        }

        public static bool Exists(string strSql, params SQLiteParameter[] cmdParms)
        {
            object single = DbHelperSQLite.GetSingle(strSql, cmdParms);
            return (object.Equals(single, (object)null) || object.Equals(single, (object)DBNull.Value) ? 0 : int.Parse(single.ToString())) != 0;
        }

        public static int ExecuteSql(string string_0)
        {
            using (SQLiteConnection connection = new SQLiteConnection(DbHelperSQLite.connectionString))
            {
                using (SQLiteCommand sqLiteCommand = new SQLiteCommand(string_0, connection))
                {
                    try
                    {
                        connection.Open();
                        return sqLiteCommand.ExecuteNonQuery();
                    }
                    catch (SQLiteException ex)
                    {
                        connection.Close();
                        throw new Exception(ex.Message);
                    }
                }
            }
        }

        public static void ExecuteSqlTran(ArrayList SQLStringList)
        {
            using (SQLiteConnection sqLiteConnection = new SQLiteConnection(DbHelperSQLite.connectionString))
            {
                sqLiteConnection.Open();
                SQLiteCommand sqLiteCommand = new SQLiteCommand();
                sqLiteCommand.Connection = sqLiteConnection;
                SQLiteTransaction sqLiteTransaction = sqLiteConnection.BeginTransaction();
                sqLiteCommand.Transaction = sqLiteTransaction;
                try
                {
                    for (int index = 0; index < SQLStringList.Count; ++index)
                    {
                        string str = SQLStringList[index].ToString();
                        if (str.Trim().Length > 1)
                        {
                            sqLiteCommand.CommandText = str;
                            sqLiteCommand.ExecuteNonQuery();
                        }
                    }
                    sqLiteTransaction.Commit();
                }
                catch (SQLiteException ex)
                {
                    sqLiteTransaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }
        }

        public static int ExecuteSql(string string_0, string content)
        {
            using (SQLiteConnection connection = new SQLiteConnection(DbHelperSQLite.connectionString))
            {
                SQLiteCommand sqLiteCommand = new SQLiteCommand(string_0, connection);
                SQLiteParameter parameter = new SQLiteParameter("@content", DbType.String);
                parameter.Value = (object)content;
                sqLiteCommand.Parameters.Add(parameter);
                try
                {
                    connection.Open();
                    return sqLiteCommand.ExecuteNonQuery();
                }
                catch (SQLiteException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    sqLiteCommand.Dispose();
                    connection.Close();
                }
            }
        }

        public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
        {
            using (SQLiteConnection connection = new SQLiteConnection(DbHelperSQLite.connectionString))
            {
                SQLiteCommand sqLiteCommand = new SQLiteCommand(strSQL, connection);
                SQLiteParameter parameter = new SQLiteParameter("@fs", DbType.Binary);
                parameter.Value = (object)fs;
                sqLiteCommand.Parameters.Add(parameter);
                try
                {
                    connection.Open();
                    return sqLiteCommand.ExecuteNonQuery();
                }
                catch (SQLiteException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    sqLiteCommand.Dispose();
                    connection.Close();
                }
            }
        }

        public static object GetSingle(string string_0)
        {
            using (SQLiteConnection connection = new SQLiteConnection(DbHelperSQLite.connectionString))
            {
                using (SQLiteCommand sqLiteCommand = new SQLiteCommand(string_0, connection))
                {
                    try
                    {
                        connection.Open();
                        object objA = sqLiteCommand.ExecuteScalar();
                        return !object.Equals(objA, (object)null) && !object.Equals(objA, (object)DBNull.Value) ? objA : (object)null;
                    }
                    catch (SQLiteException ex)
                    {
                        connection.Close();
                        throw new Exception(ex.Message);
                    }
                }
            }
        }

        public static SQLiteDataReader ExecuteReader(string strSQL)
        {
            SQLiteConnection connection = new SQLiteConnection(DbHelperSQLite.connectionString);
            SQLiteCommand sqLiteCommand = new SQLiteCommand(strSQL, connection);
            try
            {
                connection.Open();
                return sqLiteCommand.ExecuteReader();
            }
            catch (SQLiteException ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static DataSet Query(string string_0)
        {
            using (SQLiteConnection connection = new SQLiteConnection(DbHelperSQLite.connectionString))
            {
                DataSet dataSet = new DataSet();
                try
                {
                    connection.Open();
                    new SQLiteDataAdapter(string_0, connection).Fill(dataSet, "ds");
                }
                catch (SQLiteException ex)
                {
                    throw new Exception(ex.Message);
                }
                return dataSet;
            }
        }




        public static int ExecuteSql(string string_0, params SQLiteParameter[] cmdParms)
        {
            using (SQLiteConnection sqliteConnection_0 = new SQLiteConnection(DbHelperSQLite.connectionString))
            {
                using (SQLiteCommand sqliteCommand_0 = new SQLiteCommand())
                {
                    try
                    {
                        DbHelperSQLite.smethod_0(sqliteCommand_0, sqliteConnection_0, (SQLiteTransaction)null, string_0, cmdParms);
                        int num = sqliteCommand_0.ExecuteNonQuery();
                        sqliteCommand_0.Parameters.Clear();
                        return num;
                    }
                    catch (SQLiteException ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
        }

        public static void ExecuteSqlTran(Hashtable SQLStringList)
        {
            using (SQLiteConnection sqliteConnection_0 = new SQLiteConnection(DbHelperSQLite.connectionString))
            {
                sqliteConnection_0.Open();
                using (SQLiteTransaction sqliteTransaction_0 = sqliteConnection_0.BeginTransaction())
                {
                    SQLiteCommand sqliteCommand_0 = new SQLiteCommand();
                    try
                    {
                        foreach (DictionaryEntry sqlString in SQLStringList)
                        {
                            string string_0 = sqlString.Key.ToString();
                            SQLiteParameter[] sqliteParameter_0 = (SQLiteParameter[])sqlString.Value;
                            DbHelperSQLite.smethod_0(sqliteCommand_0, sqliteConnection_0, sqliteTransaction_0, string_0, sqliteParameter_0);
                            sqliteCommand_0.ExecuteNonQuery();
                            sqliteCommand_0.Parameters.Clear();
                            sqliteTransaction_0.Commit();
                        }
                    }
                    catch
                    {
                        sqliteTransaction_0.Rollback();
                        throw;
                    }
                }
            }
        }

        public static object GetSingle(string string_0, params SQLiteParameter[] cmdParms)
        {
            using (SQLiteConnection sqliteConnection_0 = new SQLiteConnection(DbHelperSQLite.connectionString))
            {
                using (SQLiteCommand sqliteCommand_0 = new SQLiteCommand())
                {
                    try
                    {
                        DbHelperSQLite.smethod_0(sqliteCommand_0, sqliteConnection_0, (SQLiteTransaction)null, string_0, cmdParms);
                        object objA = sqliteCommand_0.ExecuteScalar();
                        sqliteCommand_0.Parameters.Clear();
                        return !object.Equals(objA, (object)null) && !object.Equals(objA, (object)DBNull.Value) ? objA : (object)null;
                    }
                    catch (SQLiteException ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
        }

        public static SQLiteDataReader ExecuteReader(string string_0, params SQLiteParameter[] cmdParms)
        {
            SQLiteConnection sqliteConnection_0 = new SQLiteConnection(DbHelperSQLite.connectionString);
            SQLiteCommand sqliteCommand_0 = new SQLiteCommand();
            try
            {
                DbHelperSQLite.smethod_0(sqliteCommand_0, sqliteConnection_0, (SQLiteTransaction)null, string_0, cmdParms);
                SQLiteDataReader sqLiteDataReader = sqliteCommand_0.ExecuteReader();
                sqliteCommand_0.Parameters.Clear();
                return sqLiteDataReader;
            }
            catch (SQLiteException ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static DataSet Query(string string_0, params SQLiteParameter[] cmdParms)
        {
            using (SQLiteConnection sqliteConnection_0 = new SQLiteConnection(DbHelperSQLite.connectionString))
            {
                SQLiteCommand sqLiteCommand = new SQLiteCommand();
                DbHelperSQLite.smethod_0(sqLiteCommand, sqliteConnection_0, (SQLiteTransaction)null, string_0, cmdParms);
                using (SQLiteDataAdapter sqLiteDataAdapter = new SQLiteDataAdapter(sqLiteCommand))
                {
                    DataSet dataSet = new DataSet();
                    try
                    {
                        sqLiteDataAdapter.Fill(dataSet, "ds");
                        sqLiteCommand.Parameters.Clear();
                    }
                    catch (SQLiteException ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    return dataSet;
                }
            }
        }

        private static void smethod_0(
          SQLiteCommand sqliteCommand_0,
          SQLiteConnection sqliteConnection_0,
          SQLiteTransaction sqliteTransaction_0,
          string string_0,
          SQLiteParameter[] sqliteParameter_0)
        {
            if (sqliteConnection_0.State != ConnectionState.Open)
                sqliteConnection_0.Open();
            sqliteCommand_0.Connection = sqliteConnection_0;
            sqliteCommand_0.CommandText = string_0;
            if (sqliteTransaction_0 != null)
                sqliteCommand_0.Transaction = sqliteTransaction_0;
            sqliteCommand_0.CommandType = CommandType.Text;
            if (sqliteParameter_0 == null)
                return;
            foreach (SQLiteParameter parameter in sqliteParameter_0)
                sqliteCommand_0.Parameters.Add(parameter);
        }

        static DbHelperSQLite()
        {
            DbHelperSQLite.connectionString = GlobalConfigs.ConnectionString;
        }
    }
}
